<?php
// 应用公共文件

if(!function_exists('getRealIp')) {
    /**
     * 获取真实IP
     * @return mixed
     */
    function getRealIp()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
        } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
            $ip = $_SERVER['HTTP_X_REAL_IP'];
        }
        return $ip;
    }
}

if(!function_exists('get_friend_date')) {
    /**
     * @desc 友好时间显示
     * @param $time 时间戳
     * @param string $lang $lang 语言, cn 中文, en 英文
     * @return bool|string
     */
    function get_friend_date($time, $lang = 'cn')
    {
        if (!$time) {
            return '';
        }
        $f_date = '';
        $d = time() - intval($time);
        $ld = $time - mktime(0, 0, 0, 0, 0, date('Y')); //得出年
        $md = $time - mktime(0, 0, 0, date('m'), 0, date('Y')); //得出月
        $byd = $time - mktime(0, 0, 0, date('m'), date('d') - 2, date('Y')); //前天
        $yd = $time - mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //昨天
        $dd = $time - mktime(0, 0, 0, date('m'), date('d'), date('Y')); //今天
        $td = $time - mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); //明天
        $atd = $time - mktime(0, 0, 0, date('m'), date('d') + 2, date('Y')); //后天
        if ($lang == 'cn') {
            if ($d <= 10) {
                $f_date = '刚刚';
            } else {
                switch ($d) {
                    case $d < $atd:
                        $f_date = date('Y年m月d日', $time);
                        break;
                    case $d < $td:
                        $f_date = '后天' . date('H:i', $time);
                        break;
                    case $d < 0:
                        $f_date = '明天' . date('H:i', $time);
                        break;
                    case $d < 60:
                        $f_date = $d . '秒前';
                        break;
                    case $d < 3600:
                        $f_date = floor($d / 60) . '分钟前';
                        break;
                    case $d < $dd:
                        $f_date = floor($d / 3600) . '小时前';
                        break;
                    case $d < $yd:
                        $f_date = '昨天' . date('H:i', $time);
                        break;
                    case $d < $byd:
                        $f_date = '前天' . date('H:i', $time);
                        break;
                    case $d < $md:
                        $f_date = date('m月d日 H:i', $time);
                        break;
                    case $d < $ld:
                        $f_date = date('m月d日', $time);
                        break;
                    default:
                        $f_date = date('Y年m月d日', $time);
                        break;
                }
            }
        } else {
            if ($d <= 10) {
                $f_date = 'just';
            } else {
                switch ($d) {
                    case $d < $atd:
                        $f_date = date('Y-m-d', $time);
                        break;
                    case $d < $td:
                        $f_date = 'the day after tomorrow' . date('H:i', $time);
                        break;
                    case $d < 0:
                        $f_date = 'tomorrow' . date('H:i', $time);
                        break;
                    case $d < 60:
                        $f_date = $d . 'seconds ago';
                        break;
                    case $d < 3600:
                        $f_date = floor($d / 60) . 'minutes ago';
                        break;
                    case $d < $dd:
                        $f_date = floor($d / 3600) . 'hour ago';
                        break;
                    case $d < $yd:
                        $f_date = 'yesterday' . date('H:i', $time);
                        break;
                    case $d < $byd:
                        $f_date = 'the day before yesterday' . date('H:i', $time);
                        break;
                    case $d < $md:
                        $f_date = date('m-d H:i', $time);
                        break;
                    case $d < $ld:
                        $f_date = date('m-d', $time);
                        break;
                    default:
                        $f_date = date('Y-m-d', $time);
                        break;
                }
            }
        }
        return $f_date;

    }
}

if (!function_exists('get_guid_v4')) {

    /**
     * 获取唯一性GUID
     * @param bool $trim 是否去除{}
     * @return string 返回结果
     * @author Dai哥
     * @date 2021-10-7
     */
    function get_guid_v4($trim = true)
    {
        // Windows
        if (function_exists('com_create_guid') === true) {
            $charid = com_create_guid();
            return $trim == true ? trim($charid, '{}') : $charid;
        }
        // OSX/Linux
        if (function_exists('openssl_random_pseudo_bytes') === true) {
            $data = openssl_random_pseudo_bytes(16);
            $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
            $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
            return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
        }
        // Fallback (PHP 4.2+)
        mt_srand((double)microtime() * 10000);
        $charid = strtolower(md5(uniqid(rand(), true)));
        $hyphen = chr(45);                  // "-"
        $lbrace = $trim ? "" : chr(123);    // "{"
        $rbrace = $trim ? "" : chr(125);    // "}"
        $guidv4 = $lbrace .
            substr($charid, 0, 8) . $hyphen .
            substr($charid, 8, 4) . $hyphen .
            substr($charid, 12, 4) . $hyphen .
            substr($charid, 16, 4) . $hyphen .
            substr($charid, 20, 12) .
            $rbrace;
        return $guidv4;
    }
}

if (!function_exists('success')) {
    /**
     * 接口返回成功
     * @param array $data
     * @param string $msg
     * @return \think\response\Json
     */
    function success($data = [], $msg = '')
    {
        $response = array(
            'code' => 0,
            'message' => $msg,
            'data' => $data,
        );
        return json($response, 200, ['Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Headers' => 'x-requested-with,content-type,token',
            'Access-Control-Allow-Methods' => 'POST,GET,OPTIONS,DELETE',
            'Access-Control-Request-Method' => 'POST,GET,OPTIONS,DELETE'
        ]);
    }
}

if(!function_exists('error')) {
    /**
     * 接口返回失败
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    function error($msg = '', $data = [])
    {
        $response = array(
            'code' => 1,
            'message' => $msg,
            'data' => $data,
        );
        return json($response, 200, ['Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Headers' => 'x-requested-with,content-type,token',
            'Access-Control-Allow-Methods' => 'POST,GET,OPTIONS,DELETE',
            'Access-Control-Request-Method' => 'POST,GET,OPTIONS,DELETE'
        ]);
    }
}